<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  
  <link rel="shortcut icon" href="../../img/favicon.ico">
  <title>0001 two sum - 力扣问题题解（简易版）</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="../../css/theme.css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "0001 two sum";
    var mkdocs_page_input_path = "\u7b2c 2 \u7ae0 \u6392\u5e8f\u7b97\u6cd5/0001-two-sum.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
        <a href="../.." class="icon icon-home"> 力扣问题题解（简易版）</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../..">Welcome to MkDocs</a>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">广度优先遍历</span></p>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../../%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86/0126-word-ladder-ii/">0126 word ladder ii</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../../%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86/0127-word-ladder/">0127 word ladder</a>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">第 2 章 排序算法</span></p>
                <ul class="current">
                    <li class="toctree-l1 current"><a class="reference internal current" href="./">0001 two sum</a>
    <ul class="current">
    </ul>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../0011-container-with-most-water/">0011 container with most water</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F/">归并排序</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F/">插入排序</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F/">选择排序</a>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">第 3 章 二分查找</span></p>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../../%E7%AC%AC%203%20%E7%AB%A0%20%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/0035-search-insert-position/">0035 search insert position</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../../%E7%AC%AC%203%20%E7%AB%A0%20%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/0153-find-minimum-in-rotated-sorted-array/">0153 find minimum in rotated sorted array</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="../../%E7%AC%AC%203%20%E7%AB%A0%20%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/0154-find-minimum-in-rotated-sorted-array-ii/">0154 find minimum in rotated sorted array ii</a>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../..">力扣问题题解（简易版）</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../..">Docs</a> &raquo;</li>
    
      
        
          <li>第 2 章 排序算法 &raquo;</li>
        
      
    
    <li>0001 two sum</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h3 id="_1">方法一：暴力解法</h3>
<p>Java 代码：</p>
<pre><code class="java">public class Solution {

    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        for (int i = 0; i &lt; len - 1; i++) {
            for (int j = i + 1; j &lt; len; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        throw new RuntimeException(&quot;没有找到和为 target 的两个数。&quot;);
    }
}
</code></pre>

<p>Python3 代码：</p>
<pre><code class="pytho">
</code></pre>

<p>C++ 代码：</p>
<pre><code class="cpp">
</code></pre>

<h3 id="_2">方法二：哈希表</h3>
<p>Java 代码：</p>
<pre><code class="java">import java.util.HashMap;
import java.util.Map;

public class Solution {

    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;

        Map&lt;Integer, Integer&gt; hashMap = new HashMap&lt;&gt;(len - 1);
        hashMap.put(nums[0], 0);
        for (int i = 1; i &lt; len; i++) {
            int another = target - nums[i];
            if (hashMap.containsKey(another)) {
                return new int[]{i, hashMap.get(another)};
            }
            hashMap.put(nums[i], i);
        }
        throw new IllegalArgumentException(&quot;No two sum solution&quot;);
    }
}
</code></pre>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../0011-container-with-most-water/" class="btn btn-neutral float-right" title="0011 container with most water">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../../%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86/0127-word-ladder/" class="btn btn-neutral" title="0127 word ladder"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="../../%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86/0127-word-ladder/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../0011-container-with-most-water/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="../../js/extra.js" defer></script>
      <script src="../../js/baidu-tongji.js" defer></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
      <script src="../../search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
